{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Days Spent Together"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countDaysTogether"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计共同度过的日子数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>Alice 和 Bob 计划分别去罗马开会。</p>\n",
    "\n",
    "<p>给你四个字符串&nbsp;<code>arriveAlice</code>&nbsp;，<code>leaveAlice</code>&nbsp;，<code>arriveBob</code>&nbsp;和&nbsp;<code>leaveBob</code>&nbsp;。Alice 会在日期&nbsp;<code>arriveAlice</code>&nbsp;到&nbsp;<code>leaveAlice</code>&nbsp;之间在城市里（<strong>日期为闭区间</strong>），而 Bob 在日期&nbsp;<code>arriveBob</code>&nbsp;到&nbsp;<code>leaveBob</code>&nbsp;之间在城市里（<strong>日期为闭区间</strong>）。每个字符串都包含 5 个字符，格式为&nbsp;<code>\"MM-DD\"</code>&nbsp;，对应着一个日期的月和日。</p>\n",
    "\n",
    "<p>请你返回 Alice和 Bob 同时在罗马的天数。</p>\n",
    "\n",
    "<p>你可以假设所有日期都在 <strong>同一个</strong>&nbsp;自然年，而且 <strong>不是</strong>&nbsp;闰年。每个月份的天数分别为：<code>[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]</code>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>arriveAlice = \"08-15\", leaveAlice = \"08-18\", arriveBob = \"08-16\", leaveBob = \"08-19\"\n",
    "<b>输出：</b>3\n",
    "<b>解释：</b>Alice 从 8 月 15 号到 8 月 18 号在罗马。Bob 从 8 月 16 号到 8 月 19 号在罗马，他们同时在罗马的日期为 8 月 16、17 和 18 号。所以答案为 3 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>arriveAlice = \"10-01\", leaveAlice = \"10-31\", arriveBob = \"11-01\", leaveBob = \"12-31\"\n",
    "<b>输出：</b>0\n",
    "<b>解释：</b>Alice 和 Bob 没有同时在罗马的日子，所以我们返回 0 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>所有日期的格式均为&nbsp;<code>\"MM-DD\"</code>&nbsp;。</li>\n",
    "\t<li>Alice 和 Bob 的到达日期都 <strong>早于或等于</strong> 他们的离开日期。</li>\n",
    "\t<li>题目测试用例所给出的日期均为 <strong>非闰年</strong> 的有效日期。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-days-spent-together](https://leetcode.cn/problems/count-days-spent-together/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-days-spent-together](https://leetcode.cn/problems/count-days-spent-together/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"08-15\"\\n\"08-18\"\\n\"08-16\"\\n\"08-19\"', '\"10-01\"\\n\"10-31\"\\n\"11-01\"\\n\"12-31\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month2day=list(accumulate((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), initial=0))\n",
    "\n",
    "        def calcu_day(date:str):\n",
    "            month=int(date[:2])\n",
    "            day=int(date[-2:])\n",
    "            return month2day[month-1]+day\n",
    "\n",
    "        left=calcu_day(max(arriveAlice,arriveBob))\n",
    "        right=calcu_day(min(leaveAlice,leaveBob))\n",
    "        return max(right-left+1,0)\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        mon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        a1 = arriveAlice                                                               \n",
    "        a2 = leaveAlice                                                             \n",
    "        b1 = arriveBob                                                              \n",
    "        b2 = leaveBob                                                             \n",
    "        if a1 > b2 or b1 > a2:                                               \n",
    "            return 0                                                         \n",
    "        lst = [a1, a2, b1, b2]                                               \n",
    "        lst1 = sorted(lst)                                                   \n",
    "        x, y = lst1[1], lst1[2]                                              \n",
    "        k = 0                                                                \n",
    "        kl = []                                                              \n",
    "        for i in mon:                                                        \n",
    "            kl.append(k)                                                     \n",
    "            k = k + i                                                        \n",
    "        y1, y2 = int(y.split('-')[0]), int(y.split('-')[1])                  \n",
    "        x1, x2 = int(x.split('-')[0]), int(x.split('-')[1])                  \n",
    "        print(x1,x2,y1,y2)                                                   \n",
    "        ad = kl[y1 -1] - kl[x1-1 ] - x2 + y2  +1                             \n",
    "        return ad                                                            \n",
    "                                                                     \n",
    "                                                                     \n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "DAYS_SUM = list(accumulate((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), initial=0))\n",
    "class Solution:\n",
    "    \n",
    "\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        left = self.calc_days(max(arriveAlice, arriveBob))\n",
    "        right = self.calc_days(min(leaveAlice, leaveBob))\n",
    "        return max(right - left + 1, 0) # 答案不能为负数\n",
    "\n",
    "    def calc_days(self, date:str) -> int:\n",
    "        return DAYS_SUM[int(date[:2]) - 1] + int(date[3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        prefix_sum = list(accumulate(month, initial=0))\n",
    "        # print(prefix_sum)\n",
    "        def cal_day(data):\n",
    "            m = int(data[:2])\n",
    "            d = int(data[-2:])\n",
    "            return prefix_sum[m-1] + d\n",
    "        # aA = cal_day(arriveAlice)\n",
    "        # aB = cal_day(arriveBob)\n",
    "        # lA = cal_day(leaveAlice)\n",
    "        # lB = cal_day(leaveBob)\n",
    "        \n",
    "        # left = max(aA, aB)\n",
    "        # right = min(lA, lB)\n",
    "        \n",
    "        left = cal_day(max(arriveAlice, arriveBob))  # 直接比较字符串再计算\n",
    "        right = cal_day(min(leaveAlice, leaveBob))\n",
    "\n",
    "        return max(right-left+1, 0)\n",
    "\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        days_sum = list(accumulate((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), initial = 0))\n",
    "\n",
    "        def cal_days(date:str) -> int:\n",
    "            return days_sum[int(date[:2]) - 1] + int(date[3:])\n",
    "\n",
    "        left = cal_days(max(arriveAlice, arriveBob))\n",
    "        right = cal_days(min(leaveAlice, leaveBob))\n",
    "        return max((right - left + 1), 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        a = max(arriveAlice, arriveBob)\n",
    "        b = min(leaveAlice, leaveBob)\n",
    "        days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)\n",
    "        x = sum(days[:int(a[:2]) - 1]) + int(a[3:])\n",
    "        y = sum(days[:int(b[:2]) - 1]) + int(b[3:])\n",
    "        return max(y - x + 1, 0)\n",
    "\n",
    "# 作者：ylb\n",
    "# 链接：https://leetcode.cn/problems/count-days-spent-together/solutions/2230883/python3javacgo-yi-ti-yi-jie-mo-ni-by-lcb-lyx2/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。\n",
    "#         arm, ard = arriveAlice.split(\"-\")\n",
    "#         alm, ald = leaveAlice.split(\"-\")\n",
    "#         brm, brd = arriveBob.split(\"-\")\n",
    "#         blm, bld = leaveBob.split(\"-\")\n",
    "#         m_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "#         aa = sum(m_list[:int(arm)-1]) + int(ard)\n",
    "#         aaa = sum(m_list[:int(alm) - 1]) + int(ald)\n",
    "#         bb = sum(m_list[:int(brm) - 1]) + int(brd)\n",
    "#         bbb = sum(m_list[:int(blm) - 1]) + int(bld)\n",
    "#         a =set()\n",
    "#         b =set()\n",
    "#         for i in range(aa,aaa+1):\n",
    "#             a.add(i)\n",
    "#         for i in range(bb,bbb+1):\n",
    "#             b.add(i)\n",
    "\n",
    "#         return len(a)+len(b)-len(a.union(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        presums = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        for i in range(2, 13):\n",
    "            presums[i] += presums[i - 1]\n",
    "        a = presums[int(arriveAlice[:2]) - 1] + int(arriveAlice[-2:])\n",
    "        b = presums[int(leaveAlice[:2]) - 1] + int(leaveAlice[-2:])\n",
    "        c = presums[int(arriveBob[:2]) - 1] + int(arriveBob[-2:])\n",
    "        d = presums[int(leaveBob[:2]) - 1] + int(leaveBob[-2:])\n",
    "        return max(min(b, d) - max(a, c) + 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month_day=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if leaveAlice<arriveBob or leaveBob<arriveAlice:\n",
    "            return 0\n",
    "        arr=arriveAlice if arriveAlice>arriveBob else arriveBob\n",
    "        leave=leaveAlice if leaveAlice<leaveBob else leaveBob\n",
    "        arr_m, le_m=int(arr[0:2]),int(leave[0:2])\n",
    "        print(arr_m,le_m)\n",
    "        if arr_m==le_m:\n",
    "            return int(leave[-2:])-int(arr[-2:])+1\n",
    "        time=month_day[arr_m-1]-int(arr[-2:])+1+int(leave[-2:])+sum(month_day[arr_m:le_m-1])\n",
    "        return time\n",
    "\n",
    "\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        def quling(s:str) -> int:\n",
    "            if s[0] == '0':\n",
    "                return int(s[1])\n",
    "            else:\n",
    "                return int(s)\n",
    "        dict1 = {\"01\":0, \"02\":31, \"03\":59, \"04\":90, \"05\":120, \"06\":151, \"07\":181, \"08\":212, \"09\":243, \"10\":273, \"11\":304, \"12\":334}\n",
    "        arriveAlice = arriveAlice.split('-')\n",
    "        leaveAlice = leaveAlice.split('-')\n",
    "        arriveBob = arriveBob.split('-')\n",
    "        leaveBob = leaveBob.split('-')\n",
    "        a = dict1[arriveAlice[0]] + quling(arriveAlice[1])\n",
    "        b = dict1[leaveAlice[0]] + quling(leaveAlice[1])\n",
    "        c = dict1[arriveBob[0]] + quling(arriveBob[1])\n",
    "        d = dict1[leaveBob[0]] + quling(leaveBob[1])\n",
    "        if d < a  or c > b:\n",
    "            return 0\n",
    "        else:\n",
    "            return min(b,d) - max(c,a) + 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        x=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        t=0\n",
    "        if arriveAlice>leaveBob or arriveBob>leaveAlice:\n",
    "            return 0\n",
    "        else:\n",
    "            if arriveAlice>=arriveBob:\n",
    "                if leaveAlice<=leaveBob:\n",
    "                    b=leaveAlice\n",
    "                else:\n",
    "                    b=leaveBob\n",
    "                for i in range(int(arriveAlice[:2]), int(b[:2])):\n",
    "                    t += x[i - 1]\n",
    "                return t+int(b[3:])-int(arriveAlice[3:])+1\n",
    "\n",
    "            else:\n",
    "                if leaveAlice>=leaveBob:\n",
    "                    b=leaveBob\n",
    "                else:\n",
    "                    b=leaveAlice\n",
    "                for i in range(int(arriveBob[:2]), int(b[:2])):\n",
    "                    t += x[i - 1]\n",
    "                return t+int(b[3:])-int(arriveBob[3:])+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int: \n",
    "        amonth1=int(arriveAlice[:2])\n",
    "        aday1=int(arriveAlice[3:])\n",
    "        amonth2=int(leaveAlice[:2])\n",
    "        aday2=int(leaveAlice[3:])\n",
    "        bmonth1=int(arriveBob[:2])\n",
    "        bday1=int(arriveBob[3:])\n",
    "        bmonth2=int(leaveBob[:2])\n",
    "        bday2=int(leaveBob[3:])\n",
    "        monlist=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if amonth1 < bmonth1: resday1=bday1\n",
    "        elif amonth1 == bmonth1: resday1=max(aday1,bday1)\n",
    "        else: resday1=aday1\n",
    "        if amonth2 < bmonth2: resday2=aday2\n",
    "        elif amonth2 == bmonth2: resday2=min(aday2,bday2)\n",
    "        else: resday2=bday2\n",
    "        mbegin = max(amonth1,bmonth1)\n",
    "        mend = min(amonth2,bmonth2)\n",
    "        inbtw=0\n",
    "        for k in range(mbegin+1, mend):\n",
    "            inbtw+=monlist[k-1]\n",
    "        if mbegin < mend:   \n",
    "            return monlist[mbegin-1]+1-resday1+resday2+inbtw\n",
    "        elif mbegin == mend:\n",
    "            return max(resday2-resday1+1,0)\n",
    "        else:\n",
    "            return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        datesOfMonths=[31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        prefixSum=[0]#这是什么意思？\n",
    "        for date in datesOfMonths:\n",
    "            prefixSum.append(prefixSum[-1]+date)\n",
    "        arriveAliceDay=self.calculateDayOfYear(arriveAlice,prefixSum)\n",
    "        leaveAliceDay=self.calculateDayOfYear(leaveAlice,prefixSum)\n",
    "        arriveBobDay=self.calculateDayOfYear(arriveBob,prefixSum)\n",
    "        leaveBobDay=self.calculateDayOfYear(leaveBob,prefixSum)\n",
    "        return max(0,min(leaveAliceDay,leaveBobDay)-max(arriveAliceDay,arriveBobDay)+1)\n",
    "    def calculateDayOfYear(self,day:str,prefixSum:List[int])->int:#每个日子在一年中的第几天\n",
    "        month=int(day[:2])#因为日期格式为\"MM-DD\"格式\n",
    "        date=int(day[3:])\n",
    "        return prefixSum[month-1]+date#为一年中的第几天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month, day = [int(item) for item in arriveAlice.split(\"-\")]\n",
    "        arriveAlice = datetime.date(year=2023, month=month, day=day)\n",
    "        month, day = [int(item) for item in leaveAlice.split(\"-\")]\n",
    "        leaveAlice = datetime.date(year=2023, month=month, day=day)\n",
    "        month, day = [int(item) for item in arriveBob.split(\"-\")]\n",
    "        arriveBob = datetime.date(year=2023, month=month, day=day)\n",
    "        month, day = [int(item) for item in leaveBob.split(\"-\")]\n",
    "        leaveBob = datetime.date(year=2023, month=month, day=day)\n",
    "        ans = 0\n",
    "        while arriveAlice <= leaveAlice:\n",
    "            if arriveBob <= arriveAlice <= leaveBob:\n",
    "                ans += 1\n",
    "            arriveAlice += datetime.timedelta(days=1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        a = max(arriveAlice,  arriveBob)\n",
    "        b = min(leaveAlice, leaveBob)\n",
    "        date = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        m = sum(date[:int(a[:2]) - 1]) + int(a[3:])\n",
    "        n = sum(date[:int(b[:2]) - 1]) + int(b[3:])\n",
    "        return max(n - m + 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        def transfer(date):\n",
    "            days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "            if date[0] == \"0\":\n",
    "                month = int(date[1])\n",
    "            else:\n",
    "                month = int(date[:2])\n",
    "            if date[3] == \"0\":\n",
    "                day = int(date[4])\n",
    "            else:\n",
    "                day = int(date[3:])\n",
    "            print(sum(days[:month]))\n",
    "            print(day)\n",
    "            return sum(days[:month])+day\n",
    "        arriveAlice = transfer(arriveAlice)\n",
    "        leaveAlice = transfer(leaveAlice)\n",
    "        arriveBob = transfer(arriveBob)\n",
    "        leaveBob = transfer(leaveBob)\n",
    "        \n",
    "        if arriveAlice>arriveBob:\n",
    "            arriveAlice,leaveAlice,arriveBob,leaveBob = arriveBob,leaveBob,arriveAlice,leaveAlice\n",
    "        print(arriveBob)\n",
    "        print(leaveBob)\n",
    "        if arriveBob>leaveAlice:\n",
    "            return 0\n",
    "        elif leaveBob<=leaveAlice:\n",
    "            return leaveBob-arriveBob+1\n",
    "        else:\n",
    "            return leaveAlice-arriveBob+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        \n",
    "        def check(x):\n",
    "            m, d = [int(w) for w in x.split(\"-\")]\n",
    "            ans = 0\n",
    "            for i in range(m-1):\n",
    "                ans += days[i]\n",
    "            ans += d\n",
    "            return ans\n",
    "        a, b = check(arriveAlice), check(leaveAlice)\n",
    "        c, d = check(arriveBob), check(leaveBob)\n",
    "        low = max(a, c)\n",
    "        high = min(b, d)\n",
    "        if low <= high:\n",
    "            return high-low+1\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        # arriveAlice = \"08-15\", leaveAlice = \"08-18\", arriveBob = \"08-16\", leaveBob = \"08-19\"\n",
    "        month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        def check(s):\n",
    "            m, d = s[0:2], s[3:5]\n",
    "            m, d = int(m), int(d)\n",
    "            return sum(month[: m - 1]) + d\n",
    "        res = min(check(leaveAlice), check(leaveBob)) - max(check(arriveAlice), check(arriveBob)) + 1\n",
    "        return res if res >= 0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        alicearrivemonth=int(arriveAlice[0])*10+int(arriveAlice[1])\n",
    "        alicearriveday=int(arriveAlice[3])*10+int(arriveAlice[4])\n",
    "        aliceleavemonth = int(leaveAlice[0]) * 10 + int(leaveAlice[1])\n",
    "        aliceleaveday = int(leaveAlice[3]) * 10 + int(leaveAlice[4])\n",
    "        bobarrivemonth = int(arriveBob[0]) * 10 + int(arriveBob[1])\n",
    "        bobarriveday = int(arriveBob[3]) * 10 + int(arriveBob[4])\n",
    "        bobleavemonth = int(leaveBob[0]) * 10 + int(leaveBob[1])\n",
    "        bobleaveday = int(leaveBob[3]) * 10 + int(leaveBob[4])\n",
    "        d=[0,31,59,90,120,151,181,212,243,273,304,334]\n",
    "        alice=[d[alicearrivemonth-1]+alicearriveday,d[aliceleavemonth-1]+aliceleaveday]\n",
    "        bob=[d[bobarrivemonth-1]+bobarriveday,d[bobleavemonth-1]+bobleaveday]\n",
    "        if (alice[0]>bob[1]) or (bob[0]>alice[1]):\n",
    "            return 0\n",
    "        return (min(alice[1],bob[1])-max(alice[0],bob[0])+1)\n",
    "\n",
    "            \n",
    "\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        a = max(arriveAlice, arriveBob)\n",
    "        b = min(leaveAlice, leaveBob)\n",
    "        days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)\n",
    "        x = sum(days[:int(a[:2]) - 1]) + int(a[3:])\n",
    "        y = sum(days[:int(b[:2]) - 1]) + int(b[3:])\n",
    "        return max(y - x + 1, 0)\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "DAYS = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)\n",
    "def to_days(s):\n",
    "    return DAYS[int(s[:2]) - 1] + int(s[-2:])\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        last_arrive = max(to_days(arriveAlice), to_days(arriveBob))\n",
    "        first_leave = min(to_days(leaveAlice), to_days(leaveBob))\n",
    "        if last_arrive > first_leave:\n",
    "            return 0\n",
    "        return first_leave - last_arrive + 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month_count = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        x1 = max(arriveAlice,arriveBob)\n",
    "        x2 = min(leaveAlice,leaveBob)\n",
    "        a = sum(month_count[:(int(x1[:2])-1)]) + int(x1[3:])\n",
    "        b = sum(month_count[:(int(x2[:2])-1)]) + int(x2[3:])\n",
    "        return max(b-a+1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        datesOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        prefixSum = [0]\n",
    "        for date in datesOfMonths:\n",
    "            prefixSum.append(prefixSum[-1]+date)\n",
    "\n",
    "        arriveAliceday = self.calculateDayOfYear(arriveAlice,prefixSum)\n",
    "        leaveAliceday = self.calculateDayOfYear(leaveAlice, prefixSum)\n",
    "        arriveBobday = self.calculateDayOfYear(arriveBob, prefixSum)\n",
    "        leaveBobday = self.calculateDayOfYear(leaveBob, prefixSum)\n",
    "        return max(0,min(leaveAliceday,leaveBobday)-max(arriveAliceday,arriveBobday)+1)\n",
    "\n",
    "    def calculateDayOfYear(self,day,prefixSum):\n",
    "        month = int(day[:2])\n",
    "        date = int(day[3:])\n",
    "        return prefixSum[month-1]+date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "         \n",
    "        x=max(arriveAlice,arriveBob)\n",
    "        y=min(leaveAlice,leaveBob)\n",
    "        days=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        A=sum(days[:int(x[:2]) - 1]) + int(x[3:])\n",
    "        B=sum(days[:int(y[:2]) - 1]) + int(y[3:])\n",
    "        return max((B - A) +1,0)\n",
    "        if arriveAlice > leaveBob or arriveBob > leaveAlice:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        pref = [0]\n",
    "        for i in range(12):\n",
    "            pref.append(pref[-1] + days[i])\n",
    "        if arriveBob > leaveAlice or arriveAlice > leaveBob:\n",
    "            return 0\n",
    "        b = max(arriveAlice, arriveBob)\n",
    "        e = min(leaveAlice, leaveBob)\n",
    "        # print(b, e)\n",
    "        m1, d1 = b.split(\"-\")\n",
    "        m2, d2 = e.split(\"-\")\n",
    "        return pref[int(m2) - 1] + int(d2) - pref[int(m1) - 1] - int(d1) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import datetime\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        if leaveAlice < arriveBob or leaveBob < arriveAlice: \n",
    "            return 0\n",
    "        start, end= max(arriveAlice, arriveBob), min(leaveAlice, leaveBob)\n",
    "        start = datetime.datetime(2021, int(start[:2]), int(start[3:]))\n",
    "        end   = datetime.datetime(2021, int(end[:2]),   int(end[3:]))\n",
    "        return (end - start).days + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        datesOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        prefixSum = [0]\n",
    "        for date in datesOfMonths:\n",
    "            prefixSum.append(prefixSum[-1] + date)\n",
    "\n",
    "        arriveAliceDay = self.calculateDayOfYear(arriveAlice, prefixSum)\n",
    "        leaveAliceDay = self.calculateDayOfYear(leaveAlice, prefixSum)\n",
    "        arriveBobDay = self.calculateDayOfYear(arriveBob, prefixSum)\n",
    "        leaveBobDay = self.calculateDayOfYear(leaveBob, prefixSum)\n",
    "        return max(0, min(leaveAliceDay, leaveBobDay) - max(arriveAliceDay, arriveBobDay) + 1)\n",
    "\n",
    "    def calculateDayOfYear(self, day: str, prefixSum: List[int]) -> int:\n",
    "        month = int(day[:2])\n",
    "        date = int(day[3:])\n",
    "        return prefixSum[month - 1] + date\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "DAYS_SUM = list(accumulate((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), initial=0))\n",
    "\n",
    "def calc_days(date: str) -> int:\n",
    "    return DAYS_SUM[int(date[:2]) - 1] + int(date[3:])\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        left = calc_days(max(arriveAlice, arriveBob))\n",
    "        right = calc_days(min(leaveAlice, leaveBob))\n",
    "        return max(right - left + 1, 0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "\n",
    "        month = [31, 28, 31, 30,31,30,31,31,30,31,30,31]\n",
    "        daysum = [0]\n",
    "        for day in month:\n",
    "            daysum.append(daysum[-1]+day)\n",
    "\n",
    "        arriveAliceday = self.countDays(arriveAlice, daysum)\n",
    "        leaveAliceday = self.countDays(leaveAlice, daysum)\n",
    "        arriveBobday = self.countDays(arriveBob, daysum)\n",
    "        leaveBobday = self.countDays(leaveBob, daysum)\n",
    "\n",
    "        return max(0, min(leaveAliceday, leaveBobday) - max(arriveAliceday, arriveBobday) + 1)\n",
    "\n",
    "\n",
    "    def countDays(self, s, daysum):\n",
    "        m = int(s[:2])\n",
    "        d = int(s[3:])\n",
    "        return daysum[m-1]+d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        day_of_mon=[0,0,31,59,90,120,151,181,212,243,273,304,334]\n",
    "        aa=[int(_) for _ in arriveAlice.split('-')]\n",
    "        print(aa)\n",
    "        aa=day_of_mon[aa[0]]+aa[1]\n",
    "        al=[int(_) for _ in leaveAlice.split('-')]\n",
    "        al=day_of_mon[al[0]]+al[1]\n",
    "        alice_at=set(range(aa,al))\n",
    "\n",
    "\n",
    "        ba=[int(_) for _ in arriveBob.split('-')]\n",
    "        ba=day_of_mon[ba[0]]+ba[1]\n",
    "\n",
    "        bl=[int(_) for _ in leaveBob.split('-')]\n",
    "        print('bl',bl)\n",
    "        bl=day_of_mon[bl[0]]+bl[1]\n",
    "\n",
    "        ret=len(set(range(aa,al+1))&set(range(ba,bl+1)))\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if arriveBob>leaveAlice or arriveAlice>leaveBob:\n",
    "            return 0\n",
    "        inter_arrive = max(arriveAlice,arriveBob)\n",
    "        inter_leave = min(leaveAlice,leaveBob)\n",
    "        arr_month,arr_day = list(map(int,inter_arrive.split('-')))\n",
    "        leave_month,leave_day = list(map(int,inter_leave.split('-')))\n",
    "        print(arr_month,arr_day)\n",
    "        print(leave_month,leave_day)\n",
    "        if leave_month==arr_month:\n",
    "            return leave_day-arr_day+1\n",
    "        else:\n",
    "            total = 0\n",
    "            while arr_month<leave_month:\n",
    "                total += months[arr_month]\n",
    "                arr_month += 1\n",
    "            #到达的那个月待了 months[arr_month]-arr_day+1天\n",
    "            total -= arr_day\n",
    "            total += 1\n",
    "            #走的那个月待了leave_day天\n",
    "            total += leave_day\n",
    "            return total\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        mouth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        s = list(accumulate(mouth, initial = 0))\n",
    "        def cal(x):\n",
    "            x, y = int(x[:2]), int(x[3:])\n",
    "            ans = s[x - 1] + y\n",
    "            return ans\n",
    "\n",
    "        start_a, start_b, end_a, end_b = cal(arriveAlice), cal(arriveBob), cal(leaveAlice), cal(leaveBob)\n",
    "        if end_a < start_b or end_b < start_a:\n",
    "            return 0\n",
    "        else:\n",
    "            if start_a < start_b:\n",
    "                ans = min(end_a, end_b) - start_b + 1\n",
    "            else:\n",
    "                ans = min(end_b, end_a) - start_a + 1\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        monthToDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        monthToDays = list(accumulate(monthToDays, initial = 0))\n",
    "\n",
    "        toNumber = lambda date: monthToDays[int(date[:2]) - 1] + int(date[3:]) - 1\n",
    "        \n",
    "        arriveAlice = toNumber(arriveAlice)\n",
    "        leaveAlice = toNumber(leaveAlice)\n",
    "        arriveBob = toNumber(arriveBob)\n",
    "        leaveBob = toNumber(leaveBob)\n",
    "\n",
    "        if arriveBob <= leaveAlice <= leaveBob:\n",
    "            return leaveAlice - max(arriveBob, arriveAlice) + 1\n",
    "        if arriveAlice <= leaveBob <= leaveAlice:\n",
    "            return leaveBob - max(arriveAlice, arriveBob) + 1\n",
    "        return 0\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        def getdays(i):\n",
    "            month = int(i[:2])\n",
    "            day = int(i[3:])\n",
    "            for i in range(0,month-1):\n",
    "                day+=days[i]\n",
    "            \n",
    "            return day\n",
    "        arriveAlice = getdays(arriveAlice)\n",
    "        leaveAlice = getdays(leaveAlice)\n",
    "        arriveBob = getdays(arriveBob)\n",
    "        leaveBob = getdays(leaveBob)\n",
    "        print(arriveAlice)\n",
    "        if leaveAlice<arriveBob or leaveBob<arriveAlice:\n",
    "            return 0\n",
    "        \n",
    "        if arriveBob>arriveAlice and leaveBob < leaveAlice:\n",
    "            return leaveBob - arriveBob+1\n",
    "        elif arriveBob<arriveAlice and leaveBob > leaveAlice:\n",
    "            return leaveAlice - arriveAlice+1\n",
    "        elif arriveAlice<arriveBob<=leaveAlice<leaveBob:\n",
    "            return leaveAlice-arriveBob+1\n",
    "        return leaveBob - arriveAlice +1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        res = 0\n",
    "        for i in range(1,13):\n",
    "            month = str(i) if i>=10 else ('0'+str(i))\n",
    "            for j in range(1,days[i-1]+1):\n",
    "                d = str(j) if j>=10 else ('0'+str(j))\n",
    "                date = month+'-'+d\n",
    "                if date>=arriveAlice and date<=leaveAlice and date>=arriveBob and date<=leaveBob:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if arriveAlice > leaveBob or arriveBob > leaveAlice:\n",
    "            return 0\n",
    "        right = min(leaveAlice, leaveBob)\n",
    "        left = max(arriveAlice, arriveBob)\n",
    "        right = sum(month[: int(right[0:2])]) + int(right[-2:])\n",
    "        left = sum(month[:int(left[0:2])]) + int(left[-2:])\n",
    "        return right - left + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "DAYS = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        def to_days(s):\n",
    "            return DAYS[int(s[:2]) - 1] + int(s[-2:])\n",
    "        last_arrive = max(to_days(arriveAlice), to_days(arriveBob))\n",
    "        first_leave = min(to_days(leaveAlice), to_days(leaveBob))\n",
    "        if last_arrive > first_leave:\n",
    "            return 0\n",
    "        return first_leave - last_arrive + 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        a = max(arriveAlice, arriveBob)\n",
    "        b = min(leaveAlice, leaveBob)\n",
    "        days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)\n",
    "        x = sum(days[:int(a[:2]) - 1]) + int(a[3:])\n",
    "        y = sum(days[:int(b[:2]) - 1]) + int(b[3:])\n",
    "        return max(y - x + 1, 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        arm, ard = arriveAlice.split(\"-\")\n",
    "        alm, ald = leaveAlice.split(\"-\")\n",
    "        brm, brd = arriveBob.split(\"-\")\n",
    "        blm, bld = leaveBob.split(\"-\")\n",
    "        m_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        aa = sum(m_list[:int(arm)-1]) + int(ard)\n",
    "        aaa = sum(m_list[:int(alm) - 1]) + int(ald)\n",
    "        bb = sum(m_list[:int(brm) - 1]) + int(brd)\n",
    "        bbb = sum(m_list[:int(blm) - 1]) + int(bld)\n",
    "        a =set()\n",
    "        b =set()\n",
    "        for i in range(aa,aaa+1):\n",
    "            a.add(i)\n",
    "        for i in range(bb,bbb+1):\n",
    "            b.add(i)\n",
    "\n",
    "        return len(a)+len(b)-len(a.union(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        datesOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        prefixSum = [0]\n",
    "        for date in datesOfMonths:\n",
    "            prefixSum.append(prefixSum[-1] + date)\n",
    "\n",
    "        arriveAliceDay = self.calculateDayOfYear(arriveAlice, prefixSum)\n",
    "        leaveAliceDay = self.calculateDayOfYear(leaveAlice, prefixSum)\n",
    "        arriveBobDay = self.calculateDayOfYear(arriveBob, prefixSum)\n",
    "        leaveBobDay = self.calculateDayOfYear(leaveBob, prefixSum)\n",
    "        return max(0, min(leaveAliceDay, leaveBobDay) - max(arriveAliceDay, arriveBobDay) + 1)\n",
    "\n",
    "    def calculateDayOfYear(self, day: str, prefixSum: List[int]) -> int:\n",
    "        month = int(day[:2])\n",
    "        date = int(day[3:])\n",
    "        return prefixSum[month - 1] + date\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "DAYS_SUM = list(accumulate((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), initial=0))\n",
    "\n",
    "def calc_days(date: str) -> int:\n",
    "    return DAYS_SUM[int(date[:2]) - 1] + int(date[3:])\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        left = calc_days(max(arriveAlice, arriveBob))  # 直接比较字符串再计算\n",
    "        right = calc_days(min(leaveAlice, leaveBob))\n",
    "        return max(right - left + 1, 0)  # 答案不能为负数\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month_day=list(accumulate([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],initial=0))\n",
    "        if leaveAlice<arriveBob or leaveBob<arriveAlice:\n",
    "            return 0\n",
    "        def map_day(s):\n",
    "            m,d = map(int, s.split(\"-\"))\n",
    "            return d+month_day[m-1]\n",
    "        ans=min(map_day(leaveAlice),map_day(leaveBob))-max(map_day(arriveAlice),map_day(arriveBob))+1\n",
    "        return ans if ans>0 else 0\n",
    "\n",
    "\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        a = max(arriveAlice, arriveBob)\n",
    "        b = min(leaveAlice, leaveBob)\n",
    "        days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)\n",
    "        x = sum(days[:int(a[:2]) - 1]) + int(a[3:])\n",
    "        y = sum(days[:int(b[:2]) - 1]) + int(b[3:])\n",
    "        return max(y - x + 1, 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        max_arrive = max(arriveAlice, arriveBob)  # 字符串比较\n",
    "        min_leave = min(leaveAlice, leaveBob)\n",
    "        # print(max_arrive)\n",
    "        x = sum(months[:int(max_arrive[:2]) - 1]) + int(max_arrive[3:])\n",
    "        y = sum(months[:int(min_leave[:2]) - 1]) + int(min_leave[3:])\n",
    "        res = y - x + 1\n",
    "        return res if res > 0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        def all_day(mon, day):\n",
    "            month = [[1, 31], [2, 28], [3, 31], [4, 30], [5, 31], [6, 30], [7, 31], [8, 31], [9, 30], [10, 31], [11, 30],\n",
    "                    [12, 31]]\n",
    "\n",
    "            result = 0\n",
    "            mon = int(mon)\n",
    "            day = int(day)\n",
    "\n",
    "            for i in month:\n",
    "                if i[0] < mon:\n",
    "                    result += i[1]\n",
    "\n",
    "            result += day\n",
    "            return result\n",
    "\n",
    "        aA = arriveAlice.split('-')\n",
    "        lA = leaveAlice.split('-')\n",
    "        aB = arriveBob.split('-')\n",
    "        lB = leaveBob.split('-')\n",
    "\n",
    "        aA_change = all_day(aA[0], aA[1])\n",
    "        lA_change = all_day(lA[0], lA[1])\n",
    "        aB_change = all_day(aB[0], aB[1])\n",
    "        lB_change = all_day(lB[0], lB[1])\n",
    "        # print(aA_change, lA_change, aB_change, lB_change)\n",
    "\n",
    "        if aA_change <= aB_change <= lA_change:\n",
    "            return min(lA_change, lB_change) - aB_change + 1\n",
    "        elif aB_change <= aA_change <= lB_change:\n",
    "            return min(lA_change, lB_change) - aA_change + 1\n",
    "        else:\n",
    "            return 0\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]     \n",
    "        \n",
    "        def date2day(s: str) -> int:\n",
    "            m, d = map(int, s.split('-'))\n",
    "            return sum(days[:m-1]) + d\n",
    "        \n",
    "        alice_bob = [date2day(arriveAlice), date2day(leaveAlice), date2day(arriveBob), date2day(leaveBob)]\n",
    "\n",
    "        raw_idx = [0, 1, 2, 3]\n",
    "        # alice：0到1；bob：2到3\n",
    "        temp_list = sorted(zip(alice_bob, raw_idx))\n",
    "        _, situation = zip(*temp_list)\n",
    "        if situation in [(0,1,2,3),(2,3,0,1)]:\n",
    "            if alice_bob[situation[2]] == alice_bob[situation[1]]:\n",
    "                return 1\n",
    "            else:\n",
    "                return 0\n",
    "        else:\n",
    "            return alice_bob[situation[2]] - alice_bob[situation[1]] + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        '''本质是求区间的交集，把Alice和Bob到达区间都转换成一年内天数的闭区间\n",
    "        '''\n",
    "        month2days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        def convert_to_number(date):\n",
    "            '''把日期转换成这一年对应的天\n",
    "            '''\n",
    "            month, day = [int(i) for i in date.split('-')]\n",
    "            return sum(month2days[:month - 1]) + day\n",
    "\n",
    "        alice_interval = [\n",
    "            convert_to_number(arriveAlice), convert_to_number(leaveAlice)\n",
    "        ]\n",
    "\n",
    "        bob_interval = [\n",
    "            convert_to_number(arriveBob), convert_to_number(leaveBob)\n",
    "        ]\n",
    "\n",
    "        rst = min(alice_interval[1], bob_interval[1]) - max(alice_interval[0], bob_interval[0])\n",
    "\n",
    "        return 0 if rst < 0 else (rst + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if arriveBob>leaveAlice or arriveAlice>leaveBob:\n",
    "            return 0\n",
    "        inter_arrive = max(arriveAlice,arriveBob)\n",
    "        inter_leave = min(leaveAlice,leaveBob)\n",
    "        arr_month,arr_day = list(map(int,inter_arrive.split('-')))\n",
    "        leave_month,leave_day = list(map(int,inter_leave.split('-')))\n",
    "        if leave_month==arr_month:\n",
    "            return leave_day-arr_day+1\n",
    "        else:\n",
    "            total = 0\n",
    "            while arr_month<leave_month:\n",
    "                total += months[arr_month]\n",
    "                arr_month += 1\n",
    "            #到达的那个月待了 months[arr_month]-arr_day+1天\n",
    "            total -= (arr_day-1)\n",
    "            #走的那个月待了leave_day天\n",
    "            total += leave_day\n",
    "            return total\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        aA = [int(arriveAlice[0:2]), int(arriveAlice[3:5])]\n",
    "        lA = [int(leaveAlice[0:2]), int(leaveAlice[3:5])]\n",
    "        aB = [int(arriveBob[0:2]), int(arriveBob[3:5])]\n",
    "        lB = [int(leaveBob[0:2]), int(leaveBob[3:5])]\n",
    "        temp = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]\n",
    "        aAA = temp[aA[0] - 1] + aA[1]\n",
    "        lAA = temp[lA[0] - 1] + lA[1] \n",
    "        aBB = temp[aB[0] - 1] + aB[1]\n",
    "        lBB = temp[lB[0] - 1] + lB[1]\n",
    "        t =[0]*367\n",
    "        s = [0] *367\n",
    "        t[aAA] += 1\n",
    "        t[lAA+1] -= 1\n",
    "        t[aBB] += 1\n",
    "        t[lBB+1] -= 1\n",
    "        ans = 0\n",
    "        pre, now = 0, 0\n",
    "        for i in range(1,366):\n",
    "            now = pre + t[i]\n",
    "            pre = now\n",
    "            if now == 2:\n",
    "                ans+=1 \n",
    "        return ans\n",
    "\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        res = 0\n",
    "        for i in range(1,13):\n",
    "            month = str(i) if i>=10 else ('0'+str(i))\n",
    "            for j in range(1,days[i-1]+1):\n",
    "                d = str(j) if j>=10 else ('0'+str(j))\n",
    "                date = month+'-'+d\n",
    "                if date>=arriveAlice and date<=leaveAlice and date>=arriveBob and date<=leaveBob:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if int(arriveAlice[:2]) > int(arriveBob[:2]):\n",
    "            arr = arriveAlice\n",
    "        if int(arriveAlice[:2]) < int(arriveBob[:2]):\n",
    "            arr = arriveBob\n",
    "        elif int(arriveAlice[:2]) == int(arriveBob[:2]):\n",
    "            if int(arriveAlice[3:]) > int(arriveBob[3:]):\n",
    "                arr = arriveAlice\n",
    "            else:\n",
    "                arr = arriveBob\n",
    "        if int(leaveAlice[:2]) > int(leaveBob[:2]):\n",
    "            lea = leaveBob\n",
    "        if int(leaveAlice[:2]) < int(leaveBob[:2]):\n",
    "            lea = leaveAlice\n",
    "        elif int(leaveAlice[:2]) == int(leaveBob[:2]):\n",
    "            if int(leaveAlice[3:]) > int(leaveBob[3:]):\n",
    "                lea = leaveBob\n",
    "            elif int(leaveAlice[3:]) <= int(leaveBob[3:]):\n",
    "                lea = leaveAlice\n",
    "\n",
    "        if int(lea[:2]) < int(arr[:2]):\n",
    "            return 0\n",
    "        \n",
    "        elif int(lea[:2]) > int(arr[:2]):\n",
    "            return sum(month_day[int(arr[:2])-1:int(lea[:2])-1]) - int(arr[3:]) + int(lea[3:])+1\n",
    "        \n",
    "        else:\n",
    "            if int(lea[3:]) >= int(arr[3:]):\n",
    "                return int(lea[3:]) - int(arr[3:]) + 1\n",
    "            else:\n",
    "                return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        date = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        for i in range(1, len(date)):\n",
    "            date[i] += date[i - 1]\n",
    "        a = date[int(arriveAlice[:2]) - 1] + int(arriveAlice[3:])\n",
    "        b = date[int(leaveAlice[:2]) - 1] + int(leaveAlice[3:])\n",
    "        c = date[int(arriveBob[:2]) - 1] + int(arriveBob[3:])\n",
    "        d = date[int(leaveBob[:2]) - 1] + int(leaveBob[3:])\n",
    "        alice = set([d for d in range(a, b + 1)])\n",
    "        bob = set([d for d in range(c, d + 1)])\n",
    "        return len(alice & bob)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        def calDays(string: str) -> int:\n",
    "            res = 0\n",
    "            Months_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "            mouth = int(string.split(\"-\")[0])\n",
    "            day = int(string.split(\"-\")[1])\n",
    "            for i in range(mouth-1):\n",
    "                res = res + Months_day[i]\n",
    "            return res + day\n",
    "        \n",
    "        ans = min(calDays(leaveAlice), calDays(leaveBob)) - max(calDays(arriveAlice), calDays(arriveBob)) + 1\n",
    "        return ans if ans > 0 else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        datesOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        prefixSum = [0]\n",
    "        for date in datesOfMonths:\n",
    "            prefixSum.append(prefixSum[-1] + date)\n",
    "\n",
    "        arriveAliceDay = self.calculateDayOfYear(arriveAlice, prefixSum)\n",
    "        leaveAliceDay = self.calculateDayOfYear(leaveAlice, prefixSum)\n",
    "        arriveBobDay = self.calculateDayOfYear(arriveBob, prefixSum)\n",
    "        leaveBobDay = self.calculateDayOfYear(leaveBob, prefixSum)\n",
    "        return max(0, min(leaveAliceDay, leaveBobDay) - max(arriveAliceDay, arriveBobDay) + 1)\n",
    "\n",
    "    def calculateDayOfYear(self, day: str, prefixSum: List[int]) -> int:\n",
    "        month = int(day[:2])\n",
    "        date = int(day[3:])\n",
    "        return prefixSum[month - 1] + date\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def earlier(month1, day1, month2, day2):\n",
    "        if month1 < month2:\n",
    "            return True\n",
    "        elif month1 == month2:\n",
    "            if day1 < day2:\n",
    "                return True\n",
    "        else:\n",
    "            return False\n",
    "    \n",
    "    def from11(month, day):\n",
    "        s = 0\n",
    "        if month > 1:\n",
    "            for i in range(month - 1 ):\n",
    "                s += [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][i]\n",
    "        return s + day \n",
    "\n",
    "    def sumdays(month1, day1, month2, day2):\n",
    "        print(month1, day1, month2, day2)\n",
    "        return Solution.from11(month2, day2) - Solution.from11(month1, day1) + 1\n",
    "\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        arriveAliceMonth = int(arriveAlice[0: 1+1])\n",
    "        arriveAliceDay   = int(arriveAlice[3:])\n",
    "        arriveBobMonth   = int(arriveBob[0: 1+1])\n",
    "        arriveBobDay     = int(arriveBob[3:])\n",
    "\n",
    "        leaveAliceMonth = int(leaveAlice[0: 1+1])\n",
    "        leaveAliceDay   = int(leaveAlice[3:])\n",
    "        leaveBobMonth   = int(leaveBob[0: 1+1])\n",
    "        leaveBobDay     = int(leaveBob[3:])\n",
    "\n",
    "        if Solution.earlier(leaveAliceMonth,leaveAliceDay, arriveBobMonth, arriveBobDay) or Solution.earlier(leaveBobMonth, leaveBobDay, arriveAliceMonth, arriveAliceDay):\n",
    "            return 0\n",
    "        \n",
    "        # cal internal\n",
    "        if Solution.earlier(arriveAliceMonth,arriveAliceDay, arriveBobMonth, arriveBobDay):\n",
    "            startMonth = arriveBobMonth\n",
    "            startDay   = arriveBobDay\n",
    "        else:\n",
    "            startMonth = arriveAliceMonth\n",
    "            startDay   = arriveAliceDay\n",
    "\n",
    "        if Solution.earlier(leaveAliceMonth, leaveAliceDay, leaveBobMonth, leaveBobDay):\n",
    "            endMonth   = leaveAliceMonth\n",
    "            endDay     = leaveAliceDay\n",
    "        else:\n",
    "            endMonth    = leaveBobMonth\n",
    "            endDay     = leaveBobDay\n",
    "        \n",
    "        # sum days\n",
    "        return Solution.sumdays(startMonth, startDay, endMonth, endDay)\n",
    "\n",
    "\n",
    "       \n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "DAYS = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)\n",
    "def to_days(s):\n",
    "    return DAYS[int(s[:2]) - 1] + int(s[-2:])\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        last_arrive = max(to_days(arriveAlice), to_days(arriveBob))\n",
    "        first_leave = min(to_days(leaveAlice), to_days(leaveBob))\n",
    "        return max(first_leave - last_arrive + 1, 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        l = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        res = 0\n",
    "        #判断是否有同时在罗马\n",
    "        leaveA = int(leaveAlice[0]+leaveAlice[1]+leaveAlice[3]+leaveAlice[4])\n",
    "        leaveB = int(leaveBob[0]+leaveBob[1]+leaveBob[3]+leaveBob[4])\n",
    "        arriveA = int(arriveAlice[0]+arriveAlice[1]+arriveAlice[3]+arriveAlice[4])\n",
    "        arriveB = int(arriveBob[0]+arriveBob[1]+arriveBob[3]+arriveBob[4])\n",
    "        if arriveA>leaveB or arriveB > leaveA:\n",
    "            return 0\n",
    "        #计算月份与天数\n",
    "        start = str(max(arriveA,arriveB))\n",
    "        print(start)\n",
    "        end = str(min(leaveA,leaveB))\n",
    "\n",
    "        start_month = int(start[:-2])\n",
    "        end_month = int(end[:-2])\n",
    "        for i in range(start_month, end_month):\n",
    "            res += l[i-1]\n",
    "            print(res)\n",
    "\n",
    "        start_day = int(start[-2:])\n",
    "        end_day = int(end[-2:])\n",
    "        if start_day >= end_day:\n",
    "            res -= (start_day-end_day)\n",
    "        else:\n",
    "            res += (end_day-start_day)\n",
    "\n",
    "        return res+1\n",
    "        \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import time\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int: \n",
    "        arriveAlice = time.mktime(time.strptime(arriveAlice, '%m-%d'))\n",
    "        leaveAlice = time.mktime(time.strptime(leaveAlice, '%m-%d'))\n",
    "        arriveBob = time.mktime(time.strptime(arriveBob, '%m-%d'))\n",
    "        leaveBob = time.mktime(time.strptime(leaveBob, '%m-%d'))\n",
    "\n",
    "        diff = (min(leaveAlice, leaveBob) - max(arriveAlice, arriveBob)) / 86400 + 1\n",
    "\n",
    "        return int(diff) if diff >= 0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        a_min_day, a_max_day = self.compute_duration(arriveAlice, leaveAlice)\n",
    "        b_min_day, b_max_day = self.compute_duration(arriveBob, leaveBob)\n",
    "\n",
    "        result = min(a_max_day, b_max_day) - max(a_min_day, b_min_day)  + 1\n",
    "        return result if  result > 0 else 0\n",
    "\n",
    "    def compute_duration(self, arrive: str, leave: str):\n",
    "        a_mon, a_day = map(int, arrive.split(\"-\"))\n",
    "        l_mon, l_day = map(int, leave.split(\"-\"))\n",
    "\n",
    "        min_day = sum(month_days[:a_mon]) + a_day\n",
    "        max_day = sum(month_days[:l_mon]) + l_day\n",
    "\n",
    "        return min_day, max_day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        def checkDay(m, d):\n",
    "            res = 0\n",
    "            m, d = int(m), int(d)\n",
    "            for i in range(m - 1):\n",
    "                res += month[i]\n",
    "            return res + d\n",
    "\n",
    "        startA = checkDay(arriveAlice[:2], arriveAlice[3:])\n",
    "        endA = checkDay(leaveAlice[:2], leaveAlice[3:])\n",
    "        startB = checkDay(arriveBob[:2], arriveBob[3:])\n",
    "        endB = checkDay(leaveBob[:2], leaveBob[3:])\n",
    "        if max(startA, startB) <= min(endA, endB):\n",
    "            return min(endA, endB) - max(startA, startB) + 1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        days = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]\n",
    "        aA = days[int(arriveAlice.split('-')[0]) - 1] + int(arriveAlice.split('-')[1])\n",
    "        lA = days[int(leaveAlice.split('-')[0]) - 1] + int(leaveAlice.split('-')[1])\n",
    "        aB = days[int(arriveBob.split('-')[0]) - 1] + int(arriveBob.split('-')[1])\n",
    "        lB = days[int(leaveBob.split('-')[0]) - 1] + int(leaveBob.split('-')[1])\n",
    "        a = lA - aA\n",
    "        b = lB - aB\n",
    "        if aA > lB or lA < aB:\n",
    "            return 0\n",
    "        elif aA >= aB and lA <= lB:\n",
    "            return lA - aA + 1\n",
    "        elif aA >= aB and lA > lB:\n",
    "            return lB - aA + 1\n",
    "        elif aA < aB and lA <= lB:\n",
    "            return lA - aB + 1\n",
    "        elif aA < aB and lA > lB:\n",
    "            return lB - aB + 1\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        date = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        for i in range(1, len(date)):\n",
    "            date[i] += date[i - 1]\n",
    "        a = date[int(arriveAlice[:2]) - 1] + int(arriveAlice[3:])\n",
    "        b = date[int(leaveAlice[:2]) - 1] + int(leaveAlice[3:])\n",
    "        c = date[int(arriveBob[:2]) - 1] + int(arriveBob[3:])\n",
    "        d = date[int(leaveBob[:2]) - 1] + int(leaveBob[3:])\n",
    "\n",
    "        if b < c:\n",
    "            return 0\n",
    "        else:\n",
    "            if b <= d:\n",
    "                if a <= c:\n",
    "                    return b - c + 1\n",
    "                else:\n",
    "                    return b - a + 1\n",
    "            else:\n",
    "                if a > d:\n",
    "                    return 0\n",
    "                else:\n",
    "                    if a > c:\n",
    "                        return d - a + 1\n",
    "                    else:\n",
    "                        return d - c + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        x=max(arriveAlice,arriveBob)\n",
    "        y=min(leaveAlice,leaveBob)\n",
    "        days=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        A=sum(days[:int(x[:2]) - 1]) + int(x[3:])\n",
    "        B=sum(days[:int(y[:2]) - 1]) + int(y[3:])\n",
    "        return max((B - A) +1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        # 如果Alice 更早\n",
    "        # 更早的离开时间 减去 bob来的时间  （如果bob来之前 alice就走了 那么返回 0）\n",
    "        # 反之同理之\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "\n",
    "        def get_days(time1, time2):\n",
    "            a,b = map(int, [time1[:2], time1[3:]])\n",
    "            x,y = map(int, [time2[:2], time2[3:]])\n",
    "\n",
    "            if a == x and y < b or a > x: return 0\n",
    "            return sum([days[i-1] for i in range(a, x)]) + y - b + 1\n",
    "\n",
    "        d1 = get_days(arriveAlice, arriveBob)\n",
    "        d2 = get_days(leaveAlice, leaveBob)\n",
    "        if d1 == 0 and d2 == 0:\n",
    "            # 两次都是 bob 争先\n",
    "            return get_days(arriveAlice, leaveBob)\n",
    "        elif d1 == 0:\n",
    "            return get_days(arriveAlice, leaveAlice)\n",
    "        elif d2 == 0:\n",
    "            return get_days(arriveBob, leaveBob)\n",
    "        return get_days(arriveBob, leaveAlice)\n",
    "\n",
    "        # 先走的 减去 后来的   就算同时存在的时间 天数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "#         a = max(arriveAlice, arriveBob)\n",
    "#         b = min(leaveAlice, leaveBob)\n",
    "#         days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)\n",
    "#         x = sum(days[:int(a[:2]) - 1]) + int(a[3:])\n",
    "#         y = sum(days[:int(b[:2]) - 1]) + int(b[3:])\n",
    "#         return max(y - x + 1, 0)\n",
    "\n",
    "# 作者：ylb\n",
    "# 链接：https://leetcode.cn/problems/count-days-spent-together/solutions/2230883/python3javacgo-yi-ti-yi-jie-mo-ni-by-lcb-lyx2/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。\n",
    "        arm, ard = arriveAlice.split(\"-\")\n",
    "        alm, ald = leaveAlice.split(\"-\")\n",
    "        brm, brd = arriveBob.split(\"-\")\n",
    "        blm, bld = leaveBob.split(\"-\")\n",
    "        m_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        aa = sum(m_list[:int(arm)-1]) + int(ard)\n",
    "        aaa = sum(m_list[:int(alm) - 1]) + int(ald)\n",
    "        bb = sum(m_list[:int(brm) - 1]) + int(brd)\n",
    "        bbb = sum(m_list[:int(blm) - 1]) + int(bld)\n",
    "        a =set()\n",
    "        b =set()\n",
    "        for i in range(aa,aaa+1):\n",
    "            a.add(i)\n",
    "        for i in range(bb,bbb+1):\n",
    "            b.add(i)\n",
    "\n",
    "        return len(a)+len(b)-len(a.union(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "days_sum = list(accumulate((31,28,31,30,31,30,31,31,30,31,30,31),initial = 0))\n",
    "\n",
    "def calc_days(date):\n",
    "    return days_sum[int(date[:2])-1] + int(date[3:])\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        left = calc_days(max(arriveAlice,arriveBob))\n",
    "        right = calc_days(min(leaveAlice,leaveBob))\n",
    "        return max(right-left+1,0)\n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from dataclasses import dataclass\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        if leaveBob < arriveAlice or arriveBob > leaveAlice:\n",
    "            return 0\n",
    "\n",
    "        start = max(arriveAlice, arriveBob)\n",
    "        end = min(leaveAlice, leaveBob)\n",
    "        start_month, start_day = [int(x) for x in start.split(\"-\")]\n",
    "        end_month, end_day = [int(x) for x in end.split(\"-\")]\n",
    "\n",
    "        day_diff = end_day - start_day + 1\n",
    "        if start_month == end_month:\n",
    "            return day_diff\n",
    "\n",
    "        day_in_months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        return sum(day_in_months[start_month:end_month]) + day_diff\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "\n",
    "        def solve(s: str)->int:\n",
    "            month=[0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]\n",
    "            return month[int(s[:2])-1]+int(s[3:])\n",
    "\n",
    "        a=[]\n",
    "        b=[]\n",
    "        a.append(solve(arriveAlice))\n",
    "        a.append(solve(leaveAlice))\n",
    "        b.append(solve(arriveBob))\n",
    "        b.append(solve(leaveBob))\n",
    "        \n",
    "        if a[1]<b[0] or b[1]<a[0]: return 0\n",
    "        return min(a[1],b[1])-max(a[0],b[0])+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from dataclasses import dataclass\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        if leaveBob < arriveAlice or arriveBob > leaveAlice:\n",
    "            return 0\n",
    "\n",
    "        start = max(arriveAlice, arriveBob)\n",
    "        end = min(leaveAlice, leaveBob)\n",
    "        start_month, start_day = [int(x) for x in start.split(\"-\")]\n",
    "        end_month, end_day = [int(x) for x in end.split(\"-\")]\n",
    "\n",
    "        print(start, start_month, start_day)\n",
    "        print(end, end_month, end_day)\n",
    "\n",
    "        day_diff = end_day - start_day + 1\n",
    "        if start_month == end_month:\n",
    "            return day_diff\n",
    "\n",
    "        day_in_months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        return sum(day_in_months[start_month:end_month]) + day_diff\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "DAYS_SUM = list(accumulate((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), initial=0))\n",
    "\n",
    "def calc_days(date: str) -> int:\n",
    "    return DAYS_SUM[int(date[:2]) - 1] + int(date[3:])\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        left = calc_days(max(arriveAlice, arriveBob))  # 直接比较字符串再计算\n",
    "        right = calc_days(min(leaveAlice, leaveBob))\n",
    "        return max(right - left + 1, 0)  # 答案不能为负数\n",
    "\n",
    "# 作者：灵茶山艾府\n",
    "# 链接：https://leetcode.cn/problems/count-days-spent-together/solutions/1830920/by-endlesscheng-3r0d/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        arrive_a = self.abs_date(arriveAlice)\n",
    "        leave_a = self.abs_date(leaveAlice)\n",
    "        arrive_b = self.abs_date(arriveBob)\n",
    "        leave_b = self.abs_date(leaveBob)\n",
    "\n",
    "        res = [0] * 365\n",
    "        for i in range(min(arrive_a - 1, arrive_b - 1), max(leave_a, leave_b)):\n",
    "            if i >= arrive_a - 1 and i < leave_a:\n",
    "                res[i] += 1\n",
    "            if i >= arrive_b - 1 and i < leave_b:\n",
    "                res[i] += 2\n",
    "        return res.count(3)\n",
    "\n",
    "    def abs_date(self, date:str) -> int:\n",
    "        month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        month, day = list(map(int, date.split(\"-\")))\n",
    "        return sum(month_days[: month - 1]) + day\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "def cal_date(s):\n",
    "        return datetime.strptime(s,'%m-%d')\n",
    "class Solution:\n",
    "    \n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        start = max(arriveAlice,arriveBob)\n",
    "        end = min(leaveAlice,leaveBob)\n",
    "        return max((cal_date(end) -cal_date(start)).days + 1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        a = max(arriveAlice, arriveBob)\n",
    "        b = min(leaveAlice, leaveBob)\n",
    "        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        x = sum(days[:int(a[:2]) - 1]) + int(a[3:])\n",
    "        y = sum(days[:int(b[:2]) - 1]) + int(b[3:])\n",
    "        return max(y - x + 1, 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        aa = datetime.datetime.strptime(arriveAlice, '%m-%d')\n",
    "        la = datetime.datetime.strptime(leaveAlice, '%m-%d')\n",
    "        ab = datetime.datetime.strptime(arriveBob, '%m-%d')\n",
    "        lb = datetime.datetime.strptime(leaveBob, '%m-%d')\n",
    "        sa = set()\n",
    "        while aa <= la:\n",
    "            sa.add(aa)\n",
    "            aa += datetime.timedelta(days=1)\n",
    "        sb = set()\n",
    "        while ab <= lb:\n",
    "            sb.add(ab)\n",
    "            ab += datetime.timedelta(days=1)\n",
    "        return len(sa & sb)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\n",
    "        date=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        arriveAlice=arriveAlice.split('-')\n",
    "        leaveAlice=leaveAlice.split('-')\n",
    "        arriveAlice1=sum(date[:int(arriveAlice[0])-1])+int(arriveAlice[1])\n",
    "        leaveAlice1=sum(date[:int(leaveAlice[0])-1])+int(leaveAlice[1])\n",
    "        arriveBob=arriveBob.split('-')\n",
    "        leaveBob=leaveBob.split('-')\n",
    "        arriveBob1=sum(date[:int(arriveBob[0])-1])+int(arriveBob[1])\n",
    "        leaveBob1=sum(date[:int(leaveBob[0])-1])+int(leaveBob[1])\n",
    "        if arriveBob1>leaveAlice1 or arriveAlice1>leaveBob1:\n",
    "            return 0\n",
    "        else:\n",
    "            return min(leaveAlice1,leaveBob1)-max(arriveAlice1,arriveBob1)+1\n",
    "        \n",
    "        #return leaveAlice1\n",
    "        #return leaveBob1\n",
    "        #return arriveAlice1\n",
    "        #return arriveBob1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\r\n",
    "DAYS = [31,28,31,30,31,30,31,31,30,31,30,31]\r\n",
    "DAYS_SUM = list(accumulate(DAYS, initial=0))\r\n",
    "\r\n",
    "def parseDate(date: str) -> int:\r\n",
    "    month, day = map(int, date.split('-'))\r\n",
    "    return DAYS_SUM[month-1] + day\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:\r\n",
    "        left = max(parseDate(arriveAlice), parseDate(arriveBob))\r\n",
    "        right = min(parseDate(leaveAlice), parseDate(leaveBob))\r\n",
    "        return max(right - left + 1, 0)\r\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
